//
//  FDWebSocket.h
//  CenturyCity
//
//  Created by nsm on 2018/12/14.
//  Copyright © 2018 beisheng. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <SRWebSocket.h>

NS_ASSUME_NONNULL_BEGIN

#define _FDWebSockect   [FDWebSocket sharedInstance]

/**
 前言:
 数据归类方式分2种:
 1.按功能
 eg:吃饭(张三吃饭、李四吃饭) 买东西(张三买东西、李四买东西)...
 2.按个体
 eg:张三(吃饭、买东西...) 李四(吃饭、买东西)...
 
 !!!项目采取方式2
 */

/** main命令码*/
typedef NS_ENUM(NSInteger, FDMainCmd) {
    FDMainCmdNone = 0,      // 保留
    FDMainCmdOrder = 1,     // 订单
    FDMainCmdGPS = 2,       // GPS
    FDMainCmdSystem = 3,    // 系统消息
};

/** 订单子命令码*/
typedef NS_ENUM(NSInteger, FDSubCmdOrder) {
    FDSubCmdOrderNone = 0,              // 保留
    FDSubCmdOrderStatusChanged = 1,     // 订单状态发生变化
    FDSubCmdOrderMerchantTake = 2,      // 商家接单
    FDSubCmdOrderRiderAllot = 3,        // 分配骑手
    FDSubCmdOrderRiderPick = 4,         // 骑手配送
    FDSubCmdOrderRiderFinish = 5,       // 骑手配送完成
    FDSubCmdOrderUserCancel = 6,        // 用户取消订单
    FDSubCmdOrderMerchantUntake = 7,    // 商家8分钟未接单
    FDSubCmdOrderSystem = 8,            // 后台操作订单
    
    /*
     外卖业务调整新增订单相关命令码 2019-06-01
     暂订:
     1000~1999      用户相关
     2000~2999      商家相关
     3000~3999      骑手相关
     4000~4999      系统相关
     */
    FDSubCmdOrderUserOrphan = 1000,                 //!< 没人抢单开启孤儿模式(弹窗加价+修改配送时间)
    FDSubCmdOrderUserMarkupPrice = 1001,            //!< 用户加价(没骑手抢单)
    FDSubCmdOrderUserModifyDeliveryTime = 1002,     //!< 用户修改配送时间(没骑手抢单)
    FDSubCmdOrderUserPaid = 1003,                   //!< 订单付款成功(在线支付/货到付款(骑手|商家点完成))
    
    FDSubCmdOrderMerchantModifyPrepareTime = 2000,  //!< 商家修改备餐时间
    FDSubCmdOrderMerchantAskDelivery = 2001,        //!< 询问商家是否配送(开启自配送)
    FDSubCmdOrderMerchantStart = 2002,              //!< 商家开始配送
    FDSubCmdOrderMerchantBringUp = 2003,            //!< 商家配送(到达目的地)
    FDSubCmdOrderMerchantFinished = 2004,           //!< 商家配送完成
    FDSubCmdOrderMerchantAskTake = 2005,            //!< 商家被提醒接单
    FDSubCmdOrderMerchantUrged = 2006,              //!< 商家被催单

    FDSubCmdOrderRiderRob = 3000,                   //!< 可抢订单发生了变化(刷新抢列表/抢数量)
    FDSubCmdOrderRiderTake = 3001,                  //!< 骑手接单
    FDSubCmdOrderRiderBringUp = 3002,               //!< 骑手配送(到达目的地)
    
    FDSubCmdOrderSystemRefuse = 4000,               //!< 系统拒绝某个订单
};

/** GPS子命令码*/
typedef NS_ENUM(NSInteger, FDSubCmdGPS) {
    FDSubCmdGPSNone = 0,            // 保留
    FDSubCmdGPSRequest = 1,         // 请求下发GPS
    FDSubCmdGPSResponse = 2,        // 下发GPS数据
    FDSubCmdGPSSend = 3,            // 提交GPS
};

/** 系统消息子命令码*/
typedef NS_ENUM(NSInteger, FDSubCmdSystem) {
    FDSubCmdSystemNone = 0,         // 保留
    FDSubCmdSystemAnotherLogin = 1, // 异地登录
    
    /*
     暂订:
     0~1999          系统相关
     2000~3999       用户相关
     4000~5999       商家相关
     6000~7999       骑手相关
     */
    FDSubCmdSystemUserRechargePaid = 2000,      //!< 缴费已付款成功(由数据确定缴费信息)
    FDSubCmdSystemUserRechargeSuccess = 2001,   //!< 缴费已完成(不管人工还是自动.需区分由数据)
    FDSubCmdSystemUserRechargeCanceled = 2002,  //!< 缴费取消
    
    FDSubCmdSystemUserDidSendTransfer = 2100,   //!< 转账成功(转给别人)
    FDSubCmdSystemUserDidRecvTransfer = 2101,   //!< 收到转账(别人转给自己)
    FDSubCmdSystemUserDidRecvRefund = 2102,     //!< 收到退款
    
    FDSubCmdSystemUserDidRecvComment = 2200,    //!< 收到评论或回复(文章/评论)
    
    FDSubCmdSystemMerchantBillGenerated = 4000, //!< 账单已生成
    FDSubCmdSystemMerchantBillReviewed = 4001,  //!< 账单审核结果已出
    FDSubCmdSystemMerchantBillSettled = 4002,   //!< 账单已结账
    
    FDSubCmdSystemRiderStateReviewed = 6000,    //!< 工作状态审核结果已出
};

typedef void(^FDWebSocketDidConnectSuccess)(void);
typedef void(^FDWebSocketDidConnectFailure)(NSError *error);
typedef void(^FDWebSocketDidClose)(NSInteger code, NSString *reason, BOOL wasClean);
typedef void(^FDWebSocketDidSendData)(id data);
typedef void(^FDWebSocketDidReceiveDataBefore)(id data);
typedef void(^FDWebSocketDidReceiveData)(NSDictionary *dataDict);
typedef void(^FDWebSocketDidReceivePong)(NSData *pongPayload);
typedef void(^FDWebSocketDidReconnecting)(NSUInteger count);

@interface FDWebSocket : NSObject
// MARK: ---------- 测试Block,切勿乱用 BEGIN ----------
/** 连接成功 */
@property (nonatomic, copy) FDWebSocketDidConnectSuccess didConnectSuccess;
/** 连接失败 */
@property (nonatomic, copy) FDWebSocketDidConnectFailure didConnectFailure;
/** 连接关闭 */
@property (nonatomic, copy) FDWebSocketDidClose didClose;
/** 收到解密前消息 */
@property (nonatomic, copy) FDWebSocketDidReceiveDataBefore didReceiveDataBefore;
/** 收到解密后消息 */
@property (nonatomic, copy) FDWebSocketDidReceiveData didReceiveData;
/** 心跳回执 */
@property (nonatomic, copy) FDWebSocketDidReceivePong didReceivePong;
/** 发送数据 */
@property (nonatomic, copy) FDWebSocketDidSendData didSendData;
/** 重连回调 */
@property (nonatomic, copy) FDWebSocketDidReconnecting didReconnecting;
// MARK: ---------- 测试Block,切勿乱用 END ----------

/** 当前连接状态 */
@property (nonatomic, readonly) SRReadyState status;

+ (instancetype)sharedInstance;

/**
 连接(商家端需要多传serial:设备标识 1.用户 2.骑手 3.商家)
 @param param       参数{"uid":"用户id", "group":"端id"}
 */
- (void)connect:(NSDictionary *)param;
/**
 发送
 @param dataDict    消息体
 @param main        主模块id
 @param sub         子模块id
 */
- (void)send:(NSDictionary *)dataDict main:(int)main sub:(int)sub;
/** 关闭且不会重连 */
- (void)close;

@end

NS_ASSUME_NONNULL_END
